
//搜索、翻页
function selectFiles(pageNum) {
    $.post('manage/FileAction!selectFile.action?mypage.currentPageNum='+pageNum,
        $('#searchCriteria').serialize(),
        function (mypage) {
            $('#fileTable').html(null);
            mypage.records.forEach(function(v){
                if ($('#displayUserName').val()==null) {
                    $('#fileTable').append("<tr>" +
                        "<td>"+v.fileTitle+"</td>"+
                        "<td>"+v.userName+"</td>"+
                        "<td>"+v.fileUploadTime+"</td>"+
                        "<td>"+v.fileIntegral+"</td>"+
                        "<td>"+v.fileTypeName+"</td>"+
                        "<td>"+
                        "登录后可下载"+
                        "</td>"+
                        "</tr>"
                    )
                }else {
                    $('#fileTable').append("<tr>" +
                        "<td>"+v.fileName+"</td>"+
                        "<td>"+v.userName+"</td>"+
                        "<td>"+v.fileUploadTime+"</td>"+
                        "<td>"+v.fileIntegral+"</td>"+
                        "<td>"+v.fileTypeName+"</td>"+
                        "<td>"+
                        "<a class='btn-sm btn-primary' onclick'downlaod("+v.fileId+")'>下载</a> "+
                        "</td>"+
                        "</tr>"
                    )
                }

            });
            $('#page').html(null);
            $('#page').append("总共"+mypage.totalPageNum+"页，当前第"+mypage.currentPageNum+"页，一页"+mypage.pageSize+"行，总共"+mypage.totalRecordsNum+"行。");
            $('#page') .append("<a class='btn-sm btn-primary' onclick='selectFiles("+mypage.prevPageNum+")'>上一页</a> ");
            $('#page') .append("<a class='btn-sm btn-primary' onclick='selectFiles("+mypage.nextPageNum+")'>下一页</a>");
        }
    )
}


//下载
function downlaod(fileId) {
    //$("#"+fileId).val("下载中。。。");
    // $.post("FileAction!download?myfile.fileId="+fileId+".action",
    //     function (result) {
    //         $("#"+fileId).val("下载成功");
    //     })
    location.href = "manage/FileAction!download.action?myFile.fileId="+fileId;
}


//登录
function loginUser() {
    if (loginFormValidation()) {
        $.post("welcome/loginUser.action",
            $('#login').serialize(),
            function (data) {
                alert(data);
                switch (data) {
                    case"success":
                        location.href = "welcome/welcome.action";
                        alert("登录成功！");
                        break;
                    case"loginFailure":
                        alert("账号或密码错误！");
                        break;
                    case"accountDiscontinuation":
                        alert("账号被停用！");
                        break;
                    case"keyCodeError":
                        alert("验证码错误！");
                        break;
                }
            },"text"
        );
    }
};

//前端非空验证（标签ID，提示语）
function nonEmpty(input, tips) {
    var inputval = $("#" + input).val();
    if (!inputval) {
        layer.tips('请输入'+tips+'!', '#' + input, {tips: [2, '#CC3333']});
        return false;
    }
    return true;
}

//用户名验证
function userAccountCheck(input, tips){
    if(nonEmpty(input, tips)){
        var userAccount = $("#userAccount").val();
        $.ajax({
            url : "frontUser/userAccountCheck.action",
            data : {
                "userAccount" : userAccount
            },
            type : "post",
            dataType : "text",
            success : function (result) {
                if (result=="error") {
                    layer.tips('用户名可用！', '#account11', {tips: [2, '#00ff00']});
                } else if (result=="success") {
                    layer.tips('用户名已被使用！', '#account11', {tips: [2, '#CC3333']});
                }
            }
        });
    }
}

//密码验证
function pwdCheck(input, tips){
    if (nonEmpty(input, tips)) {
        var password = $("#password").val();
        var password2 = $("#password2").val();
        console.log("66666")
        if (password != password2) {
            layer.tips('密码不一致！', '#password2', {tips: [2, '#CC3333']});
        }
    }
}

//验证年龄
function ageCheck(input, tips){
    if(nonEmpty(input, tips)){
        var userAge = $("#userAge").val();
        if(userAge > 150 || userAge <= 0){
            layer.tips('年龄格式有误！', '#userAge', {tips: [2, '#CC3333']});
        };

    }
}

//手机验证
function modeleCheck(input, tips){
    if (nonEmpty(input, tips)) {
        var inputval = $("#"+input).val();
            if (!inputval.match(/^1[34578]\d{9}$/)) {
                layer.tips('请输入正确的'+tips+'！', '#'+input, {tips: [2, '#CC3333']});
            }
    }
}

//验证卡号
function cardNumberCheck(input, tips){
    if(nonEmpty(input, tips)){
        /*var cardNumber = $("#cardNumber").val();
        $.ajax({
            url : "manageUser/cardNumberCheck.action",
            data : {
                "cardNumber" : cardNumber
            },
            type : "post",
            dataType : "text",
            success : function (result) {
                if (result=="error") {
                    layer.tips('该卡可以使用！', '#cardNumber11', {tips: [2, '#00ff00']});

                } else if (result=="success") {
                    layer.tips('该卡已被使用！', '#cardNumber11', {tips: [2, '#CC3333']});
                }
            }
        });*/
    }
}

//验证身份证
function identityIdCheck(input, tips){
    if(nonEmpty(input, tips)){
        var inputval = $("#"+input).val();
        console.log(inputval.length);
        // if (!inputval.match(/^[12345789]\d{17}$/) || !inputval.match(/^[12345789]\d{14}$/)) {
        if (!inputval.match(/^[12345789]+$/) || !inputval.match(/^[12345789]\d{14}$/)) {
            if (inputval.length != 15 && inputval.length != 18){
                layer.tips('请输入正确的'+tips+'！', '#'+input, {tips: [2, '#CC3333']});
            }
        }
        // selectUserName(true);
    }
}

//邮箱验证
function emailCheck(input, tips){
    if (nonEmpty(input, tips)) {
        var inputval = $("#"+input).val();
            if ((!inputval.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))) {
                layer.tips('请输入正确的'+tips+'！', '#'+input, {tips: [2, '#CC3333']});
            }
    }
}

    //注册
   function registerBtn() {
        if (registerFormValidation()){
            $.post("manage/UserAction!addUser.action",
                $('#registerForm').serialize(),
                function (data) {
                    switch (data) {
                        case"true":
                            alert("注册成功！");
                            location.href = "welcome.action";
                            break;
                        case"false":
                            alert("注册失败！");
                            break;
                    }
                }
            )
        }
    };

//验证用户名是否已经存在
function selectUserName(state) {
    var result1;
    //false同步，true异步
    $.ajaxSettings.async = state;
    $.post("manage/UserAction!selectUserName.action",
        $('#registerForm').serialize(),
        function (result) {
            if (result=="false") {
                if (state){
                    layer.tips('用户名可用！', '#registerName', {tips: [2, '#00ff00']});
                }
            } else if (result=="true") {
                layer.tips('用户名已被使用！', '#registerName', {tips: [2, '#CC3333']});
            }
            result1=!result;
        }
    )
    if (!state){
        return result1;
    }
}

//更换验证码
function getCode() {
    $("#imgCode").attr("src", "imageAction/imgCode.action?d=" + new Date().valueOf());
}

//登录表单验证
function loginFormValidation() {
    var loginName = $("#loginName").val();
    var loginPwd = $("#loginPwd").val();
    var keyCode = $("#keyCode").val();
    if (loginName === "" || loginName === null) {
        layer.tips('请输入用户名！', '#loginName', {tips: [2, '#CC3333']});
        return false;
    }
    if (loginPwd === "" || loginPwd === null) {
        layer.tips('请输入密码！', '#loginPwd', {tips: [2, '#CC3333']});
        return false;
    }
    if (keyCode === "" || keyCode === null) {
        layer.tips('请输入验证码！', '#keyCode', {tips: [2, '#CC3333']});
        return false;
    }
    return true;
}

//注册表单验证
function registerFormValidation() {
    var registerName = $("#registerName").val();
    var registerPwd1 = $("#registerPwd1").val();
    var registerPwd = $("#registerPwd").val();
    var registerMobile = $("#registerMobile").val();
    var registerEmail = $("#registerEmail").val();
    var mobile = /^1[34578]\d{9}$/;
    var email = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;

    if (registerName === "" || registerName === null) {
        layer.tips('请输入用户名！', '#registerName', {tips: [2, '#CC3333']});
        return false;
    }
    if (selectUserName(false)) {
        return false;
    }
    if (registerPwd1 === "" || registerPwd1 === null) {
        layer.tips('请输入密码！', '#registerPwd1', {tips: [2, '#CC3333']});
        return false;
    }
    if (registerPwd === "" || registerPwd === null) {
        layer.tips('请确认密码！', '#registerPwd', {tips: [2, '#CC3333']});
        return false;
    }
    if (registerPwd != registerPwd1) {
        layer.tips('请密码不一致！', '#registerPwd', {tips: [2, '#CC3333']});
        return false;
    }
    if (registerMobile === "" || registerMobile === null) {
        layer.tips('请输入手机号！', '#registerMobile', {tips: [2, '#CC3333']});
        return false;
    }
    if (!registerMobile.match(mobile)) {
        layer.tips('请输入正确的手机号！', '#registerMobile', {tips: [2, '#CC3333']});
        return false;
    }
    if (registerEmail === "" || registerEmail === null) {
        layer.tips('请输入邮箱！', '#registerEmail', {tips: [2, '#CC3333']});
        return false;
    }
    if ((!registerEmail.match(email))) {
        layer.tips('请输入正确的邮箱！', '#registerEmail', {tips: [2, '#CC3333']});
        return false;
    }
    return true;
}
